package com.omesoft.hksnore.loadpage.permision;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.v4.content.ContextCompat;

import com.omesoft.hksnore.util.L;

import java.util.ArrayList;
import java.util.List;

/**
 * 检查权限的工具类
 */
public class PermissionsChecker {
    private final Context mContext;
    public static String[] PERMISSIONS = new String[]{
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.MODIFY_AUDIO_SETTINGS, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_EXTERNAL_STORAGE
            , Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.BLUETOOTH
            , Manifest.permission.BLUETOOTH_ADMIN, Manifest.permission.READ_PHONE_STATE
    };

    public PermissionsChecker(Context context) {
        mContext = context.getApplicationContext();
    }

//    // 判断权限集合
//    public boolean lacksPermissions(String... permissions) {
//        for (String permission : permissions) {
//            if (lacksPermission(permission)) {
//                return true;
//            }
//        }
//        return false;
//    }

    // 判断权限集合
    public String[] getPermissions(String... permissions) {
        List<String> tempLis = new ArrayList<String>();
        L.e("xx", "permissions " + permissions.length);
        for (String permission : permissions) {
            if (lacksPermission(permission)) {

                tempLis.add(permission);
            }
        }
        L.e("xx", "tempLis.size() " + tempLis.size());
        String[] temp = new String[tempLis.size()];
        for (int i = 0; i < tempLis.size(); i++) {
            temp[i] = tempLis.get(i);
        }
        PERMISSIONS = temp;
        return temp;

    }

    // 判断是否缺少权限
    private boolean lacksPermission(String permission) {
        return ContextCompat.checkSelfPermission(mContext, permission) ==
                PackageManager.PERMISSION_DENIED;
    }
}
